package com.dgut.lotterySystem.common.interceptor;

import com.dgut.lotterySystem.common.utils.JWTUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class LoginInterceptor implements HandlerInterceptor {

    private static final Logger logger= LoggerFactory.getLogger(LoginInterceptor.class);
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取请求头
        String token=request.getHeader("user_token");
        logger.info("获取token:"+token);
        logger.info("获取token路径:"+request.getRequestURI());
        //解析token
        Claims claim= JWTUtil.parseJWT(token);
        if(claim==null){
            logger.error("解析JWT令牌失败");
            response.setStatus(401);
            return false;
        }
        logger.info("解析JWT令牌成功");
        return true;

    }
}
